package com.jk11a.shangdaast.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.jk11a.shangdaast.subject.SubjectBean;


public class SubjectDao {

	public List<SubjectBean> getSubject(int id){
		
		List<SubjectBean> list=new ArrayList<SubjectBean>();
		try {
			String urlString = "http://192.168.1.102:8080/ShangDaAstServer/loadsubject";
			HttpPost httpPost = new HttpPost(urlString);  
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
			 nameValuePair.add(new BasicNameValuePair("id",Integer.toString(id)));
			 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);  
			 String retSrc = EntityUtils.toString(httpResponse.getEntity());
			 JSONObject result = new JSONObject( retSrc);  
			 JSONArray jsonArray = result.getJSONArray("subject");
			 int iSize = jsonArray.length();  
			 for (int i = 0; i < iSize; i++) {  
				 JSONObject jsonObj = jsonArray.getJSONObject(i);
				 SubjectBean subjectBean=new SubjectBean();
				 subjectBean.setId(Integer.parseInt(jsonObj.getJSONObject("id").toString()));
				 subjectBean.setSubjectname(jsonObj.getJSONObject("subjectname").toString());
				 subjectBean.setTime(jsonObj.getJSONObject("time").toString());
				 subjectBean.setTeacher(jsonObj.getJSONObject("teacher").toString());
				 subjectBean.setWeekday(Integer.parseInt(jsonObj.getJSONObject("weekday").toString()));
				 subjectBean.setLocation(jsonObj.getJSONObject("location").toString());
				 subjectBean.setBegintime(jsonObj.getJSONObject("begintime").toString());
				 subjectBean.setEndtime(jsonObj.getJSONObject("endtime").toString());
				 subjectBean.setNote(jsonObj.getJSONObject("note").toString());
				 list.add(subjectBean);
			 }
			 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}
}
